# coding=utf-8

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from web.models.user.auth.auth import Auth


@route(r'/user/login/?', name='user.login')
class IndexHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self):
        self.render("user/auth/denglu.html",)

    @asynchronous
    @gen.coroutine
    def post(self):
        school_id = self.get_argument('schoolId', default=self.school_id)
        name = self.get_argument('name', default='')
        password = self.get_argument('password', default='')

        ret = yield Auth().login_student(name, password, school_id)
        # print ret
        if len(ret) < 1:
            self.redirect('/user/login')
        else:
            self.set_scookie('user', ret[0])
            self.redirect('/user/student')


@route(r'/user/logout/?', name='user.logout')
class LogoutHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self):
        self.del_scookie('user')
        self.redirect('/user/login')
